﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Sobiens.Connector.Admin.BLL
{
    //TODO: refactor this, is duplicated
    [CollectionDataContract(Namespace = "", Name = "Configurations")]
    public class AllConfigurations : List<AppConfiguration>
    {
        private static DataContractSerializer _dcs = new DataContractSerializer(typeof(AllConfigurations));
        private static DataContractJsonSerializer _dcjs = new DataContractJsonSerializer(typeof(AllConfigurations));

        public String GetJSON()
        {
            return Serialize(_dcjs);
        }

        public static AllConfigurations CreateFromJSON(String json)
        {
            return Deserialize(_dcjs, json);
        }

        #region private methods

        private static AllConfigurations Deserialize(XmlObjectSerializer serializer, String data)
        {
            AllConfigurations appConfig = serializer.ReadObject(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(data))) as AllConfigurations;
            return appConfig;
        }

        private String Serialize(XmlObjectSerializer serializer)
        {
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, this);
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        #endregion
    }
}
